// pages/tabBar/project/project.js
import { request, debounce } from "../../../utils/util.js"
const { $Message } = require('../../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    projectListOrigin: [], //项目列表(原始)
    projectList: [],//项目列表
    isLoading: true,   //加载
    classifyType: "project", //类型
    visible1: false,  //以下均用于右边三个小点内容
    visible5: false,
    actions1: [
      {
        name: '邀请成员',

      },
      {
        name: '解散项目',
        color: "red"
      },
    ],
    actions5: [
      {
        name: '取消'
      },
      {
        name: '删除',
        color: '#ed3f14',
        loading: false
      }
    ],
    deleteId: "",
    value: "",    //搜索内容
    class: {            //清楚按钮的样式控制
      clear: "clear"
    },
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setStorageSync('location', "project")
    this.setData({
      ...this.data,
      value: "",
      class: {            
        clear: "clear"
      },
    })
    this.getFileList();
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getFileList();
  },

  /**
   * 获取数据的封装
   */
  getFileList() {
    const that = this
    request("GET", "/api/service/projectinfo/").then(res => {
      that.setData({
        ...that.data,
        projectListOrigin: res.data.data,
        projectList: res.data.data,
        isLoading: false
      })
      wx.stopPullDownRefresh()

    })
  },
  /**
   * 打开action
   */
  open: function (e) {

    this.setData({
      ...this.data,
      visible1: true,
      deleteId: e.currentTarget.dataset.id
    })
  },
  /**
    * 关闭上啦菜单
    */
  handleCancel1() {
    this.setData({
      visible1: false
    });
  },
  /**
   * 上啦框里的各个事件
   * @param {}} param0 
   */
  handleClickItem1({ detail }) {
    const index = detail.index + 1
    switch (index) {
      case 1:

        break;
      case 2:
        this.setData({
          ...this.data,
          visible5: true
        });
        break;
      default:
        break;
    }
  },
  /**
   * 进到内部
   * @param {} e 
   */
  handleIn(e) {

    const isRoot = "true";
    wx.navigateTo({
      url: `/pages/tabBar/project/components/projectFolderpage/index?classifyType=${this.data.classifyType}&project=${e.currentTarget.id}&name=${e.currentTarget.dataset.name}&key=${e.currentTarget.dataset.upparent}&isRoot=${isRoot}`,
    })
  },
  /**
   * 解散项目
   */
  delete: function ({ detail }) {
    const that = this;

    if (detail.index === 0) {
      this.setData({
        ...this.data,
        visible5: false
      })
    } else {
      const action = [...this.data.actions5]
      action[1].loading = true;
      this.setData({
        ...this.data,
        visible5: false,
        actions5: action
      })
      request("DELETE", `/api/service/projectinfo/${this.data.deleteId}/`)
        .then(res => {
          if (res.statusCode === 200) {
            action[1].loading = false;
            that.setData({
              ...that.data,
              visible5: false,
              actions5: action
            });
            that.getFileList();
            $Message({
              content: '解散成功！',
              type: 'success'
            });
          }
        })
    }
  },
  /**
* 键盘输入事件
*/
  input: debounce(function (e) {
    this.setData({
      ...this.data,
      value: e.detail.value
    })
    if (e.detail.value.length > 0) {
      this.setData({
        ...this.data,
        class: {
          clear: "clear-show"
        }
      })
    } else {
      this.setData({
        class: {
          clear: "clear"
        }
      })
    }
  },
    function (e) {
      const value = this.data.value
      const List = this.data.projectListOrigin
      let newList = []
      List.forEach(el => {
        if (el.name.indexOf(value) != -1) {
          newList.push(el)
        }
      })
      this.setData({
        ...this.data,
        projectList:newList
      })
    }, 300
  ),
  /**
 * 清除
 */
  clear: function () {
    this.setData({
      ...this.data,
      value: "",
      class: {
        clear: "clear"
      },
      projectList:this.data.projectListOrigin
    })
  },

})